package org.github.manx98.jlibhac.tools.fssystem.ncautils;

import org.github.manx98.jlibhac.common.math.IByteUtil;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/3 23:55
 */
public class NcaFsIntegrityInfoSha256 {
    private final byte[] data;
    private final Sha256Struct sha256Struct;

    public NcaFsIntegrityInfoSha256(byte[] data) {
        this.sha256Struct = new Sha256Struct();
        IByteUtil.mapToStruct(sha256Struct, data, 0);
        this.data = data;
    }

    public Sha256Level getLevelInfo(int index) {
        validateLevelIndex(index);
        int offset = Sha256Struct.Sha256LevelOffset + Sha256Level.Sha256LevelSize * index;
        Sha256Level sha256Level = new Sha256Level();
        IByteUtil.mapToStruct(sha256Level, data, offset);
        return sha256Level;
    }

    private void validateLevelIndex(int index) {
        if (index < 0 || index > 5) {
            throw new IllegalArgumentException("IVFC level index must be between 0 and 5. Actual: " + index);
        }
    }

    public long getLevelOffset(int index) {
        return getLevelInfo(index).Offset;
    }

    public long getLevelSize(int index) {
        return getLevelInfo(index).Size;
    }

    public byte[] getMasterHash() {
        return IByteUtil.slice(data, Sha256Struct.MasterHashOffset, Sha256Struct.MasterHashSize);
    }

    public int getBlockSize() {
        return sha256Struct.BlockSize;
    }
}
